package com.monika.main.database;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Author whh
 * @Date: 2023/04/14/ 22:11
 * @description
 */
@Component
@ConfigurationProperties(prefix = "primal.ds")
public class DataSourceConfigProperties {

    private String url;

    private String driver;

    private String userName;

    private String passwd;

    private String validationQuery = "select 1 from dual";

    //连接池初始大小
    private String initSize = "3";

    //最小连接数
    private String minIdle = "3";

    //最大连接数
    private String maxActive ="10";

    //是否开启表的初始化检查
    private boolean checkTable = false;

    //获取连接的最大等待时间单位为毫秒
    private long maxWaitTime;

    //空闲检测时间单位为毫秒
    private long evictionRunTime;

    //连接在池中最小生存时间单位毫秒
    private long minEvictionIdleTime;

    //
    private boolean keepAlive;

    private boolean testWhileIdle;

    //获取连接时是否检测连接有效
    private boolean testOnBorrow;

    //连接归还时是否检测连接有效
    private boolean testOnReturn;

    //是否缓存PrepareStatement
    private boolean poolPreparedStatement;

    //每个连接的  PrepareStatement缓存大小
    private int maxPoolPreparedStatementSizePerConnection;

    //连接检测超时设置
    private int validateQueryTimeOut;

    public String getUrl() {
        validate(url);
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriver() {
        validate(driver);
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public String getInitSize() {
        return initSize;
    }

    public void setInitSize(String initSize) {
        this.initSize = initSize;
    }

    public String getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(String minIdle) {
        this.minIdle = minIdle;
    }

    public String getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(String maxActive) {
        this.maxActive = maxActive;
    }

    public boolean isCheckTable() {
        return checkTable;
    }

    public void setCheckTable(boolean checkTable) {
        this.checkTable = checkTable;
    }

    public long getMaxWaitTime() {
        return maxWaitTime;
    }

    public void setMaxWaitTime(long maxWaitTime) {
        this.maxWaitTime = maxWaitTime;
    }

    public long getEvictionRunTime() {
        return evictionRunTime;
    }

    public void setEvictionRunTime(long evictionRunTime) {
        this.evictionRunTime = evictionRunTime;
    }

    public long getMinEvictionIdleTime() {
        return minEvictionIdleTime;
    }

    public void setMinEvictionIdleTime(long minEvictionIdleTime) {
        this.minEvictionIdleTime = minEvictionIdleTime;
    }

    public boolean isKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(boolean keepAlive) {
        this.keepAlive = keepAlive;
    }

    public boolean isTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }

    public boolean isTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public boolean isTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public boolean isPoolPreparedStatement() {
        return poolPreparedStatement;
    }

    public void setPoolPreparedStatement(boolean poolPreparedStatement) {
        this.poolPreparedStatement = poolPreparedStatement;
    }

    public int getMaxPoolPreparedStatementSizePerConnection() {
        return maxPoolPreparedStatementSizePerConnection;
    }

    public void setMaxPoolPreparedStatementSizePerConnection(int maxPoolPreparedStatementSizePerConnection) {
        this.maxPoolPreparedStatementSizePerConnection = maxPoolPreparedStatementSizePerConnection;
    }

    public int getValidateQueryTimeOut() {
        return validateQueryTimeOut;
    }

    public void setValidateQueryTimeOut(int validateQueryTimeOut) {
        this.validateQueryTimeOut = validateQueryTimeOut;
    }

    private void validate(String val){
        if(val == null || "".equals(val)){
            throw new IllegalArgumentException("数据库参数配置异常...");
        }
    }

    @Override
    public String toString() {
        return "DataSourceConfigProperty{" +
                "url='" + url + '\'' +
                ", driver='" + driver + '\'' +
                ", userName='" + userName + '\'' +
                ", passwd='" + passwd + '\'' +
                ", validationQuery='" + validationQuery + '\'' +
                ", initSize='" + initSize + '\'' +
                ", minIdle='" + minIdle + '\'' +
                ", maxActive='" + maxActive + '\'' +
                ", checkTable=" + checkTable +
                ", maxWaitTime=" + maxWaitTime +
                ", evictionRunTime=" + evictionRunTime +
                ", minEvictionIdleTime=" + minEvictionIdleTime +
                ", keepAlive=" + keepAlive +
                ", testWhileIdle=" + testWhileIdle +
                ", testOnBorrow=" + testOnBorrow +
                ", testOnReturn=" + testOnReturn +
                ", poolPreparedStatement=" + poolPreparedStatement +
                ", maxPoolPreparedStatementSizePerConnection=" + maxPoolPreparedStatementSizePerConnection +
                ", validateQueryTimeOut=" + validateQueryTimeOut +
                '}';
    }
}
